//
//  ChooseConditionView.m
//  CarUser
//
//  Created by 王森林 on 2019/1/10.
//  Copyright © 2019年 王森林. All rights reserved.
//

#import "ChooseConditionView.h"

@interface ChooseConditionView()
@property  (nonatomic, copy) void (^resultBlock)(id sales);
@property (nonatomic, strong) NSString *sales;//1为在售,2为不在售
@end

#define BTNTAG  10000000

@implementation ChooseConditionView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.sales = 0;
        self.backgroundColor = [UIColor whiteColor];
     
    }
    return self;
}

- (void)createView
{
    [self removeAllSubviews];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, 60, 20)];
    titleLabel.text = @"在售情况";
    titleLabel.textColor = Common_TextLowColor;
    titleLabel.textAlignment = NSTextAlignmentLeft;
    titleLabel.font = FONT_SIZE(14);
    [self addSubview:titleLabel];
    
    UIButton *saledBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    saledBtn.frame = CGRectMake(100, 8, 50, 44);
    [saledBtn setTitle:@"在售" forState:UIControlStateNormal];
    [saledBtn setTitleColor:Common_TextColor forState:UIControlStateNormal];
    [saledBtn  setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    [saledBtn setBackgroundColor:Common_BackgroundGrayColor];
    saledBtn.tag = BTNTAG;
    saledBtn.titleLabel.font = FONT_SIZE(15);
    [saledBtn addTarget:self action:@selector(handleBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:saledBtn];
    
    
    
    UIButton *saleNoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    saleNoBtn.frame = CGRectMake(160, 8, 70, 44);
    [saleNoBtn setTitle:@"不在售" forState:UIControlStateNormal];
    [saleNoBtn setTitleColor:Common_TextColor forState:UIControlStateNormal];
    [saleNoBtn  setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    [saledBtn setBackgroundColor:Common_BackgroundGrayColor];
    saleNoBtn.titleLabel.font = FONT_SIZE(15);
    saleNoBtn.tag = BTNTAG+1;
    [saleNoBtn addTarget:self action:@selector(handleBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:saleNoBtn];
    
    UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 59.5, 0, 0.5)];
    lineLabel.backgroundColor = Common_SepeColor;
    [self addSubview:lineLabel];
    
    
    UIButton *resetBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    resetBtn.frame = CGRectMake(0, 60, 80, 44);
    [resetBtn setTitle:@"重置" forState:UIControlStateNormal];
    [resetBtn setTitleColor:Common_TextColor forState:UIControlStateNormal];
    resetBtn.titleLabel.font = FONT_SIZE(15);
    resetBtn.tag = BTNTAG+2;
    [resetBtn addTarget:self action:@selector(handleBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:resetBtn];
    
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sureBtn.frame = CGRectMake(80, 60,  BOUNDWIDTH  - 60, 44);
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [sureBtn setTitleColor:Common_TextColor forState:UIControlStateNormal];
    [sureBtn setBackgroundColor:[UIColor  yellowColor]];
    sureBtn.titleLabel.font = FONT_SIZE(15);
    sureBtn.tag = BTNTAG+3;
    [sureBtn addTarget:self action:@selector(handleBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:sureBtn];

    
}

- (void)handleBtnAction:(id)sender
{
    UIButton *btn = sender;
    NSInteger tag = btn.tag - BTNTAG;
    
    switch (tag) {
        case 0:
        {
            // 在售
            btn.selected  = YES;
//            [btn setBackgroundColor:[UIColor redColor]];
            btn.layer.borderColor = [UIColor redColor].CGColor;
            btn.layer.borderWidth = 1;
            UIButton *otherBtn = [self viewWithTag:BTNTAG +1];
            otherBtn.selected  = NO;
            [otherBtn setBackgroundColor:Common_BackgroundGrayColor];
            otherBtn.layer.borderWidth = 0;
            self.sales = @"1";
            
        }
            break;
        case 1:
        {
             // 不在售
            btn.selected  = YES;
            btn.layer.borderColor = [UIColor redColor].CGColor;
            btn.layer.borderWidth = 1;
//            [btn setBackgroundColor:[UIColor redColor]];
            UIButton *otherBtn = [self viewWithTag:BTNTAG];
            otherBtn.selected  = NO;
            [otherBtn setBackgroundColor:Common_BackgroundGrayColor];
             otherBtn.layer.borderWidth = 0;
            self.sales =  @"2";
            
        }
            break;
        case 2:
        {
            // 重置
            UIButton *otherBtn = [self viewWithTag:BTNTAG];
            otherBtn.selected  = NO;
            [otherBtn setBackgroundColor:Common_BackgroundGrayColor];
            otherBtn.layer.borderWidth = 0;
         
            UIButton *otherBtn1 = [self viewWithTag:BTNTAG +1];
            otherBtn1.selected  = NO;
            [otherBtn1 setBackgroundColor:Common_BackgroundGrayColor];
            otherBtn1.layer.borderWidth = 0;
            self.sales = @"0";
            
        }
            break;
        case 3:
        {
            // 确定
            
            if (self.resultBlock) {
                self.resultBlock(self.sales);
            }
            
        }
            break;
            
        default:
            break;
    }
    
    
}


- (void)showViewWithBlock:(void (^)(id _Nonnull))result
{
    [self createView];
    self.resultBlock = result;
//    if (self.resultBlock) {
//        self.resultBlock(@"1");
//    }
}


- (void)dissView
{
    
    
}

@end
